package com.lazy.app.ui.act;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.core.content.res.ResourcesCompat;

import com.lazy.app.R;
import com.lazy.app.databinding.ActivityH5Binding;
import com.lazy.app.h5.LazyH5API;
import com.lazy.app.presenter.H5Presenter;
import com.zndroid.base.ui.common.CommonActivity;
import com.zndroid.bridge.InvokeController;
import com.zndroid.snackbar.Snackbar;
import com.zndroid.snackbar.SnackbarManager;
import com.zndroid.widget.title.TitleBar;

/**
 * H5页面，可以配置一些简单的网页，比如用户协议啥的
 *
 * @author lzy
 */
public class H5Activity extends CommonActivity<ActivityH5Binding, H5Presenter> {
    private Bundle savedInstanceState;
    private InvokeController invokeController;

    @Override
    protected int onSupplyStatusBarColor() {
        return ResourcesCompat.getColor(getResources(), R.color.color_white, getTheme());
    }

    @Override
    protected ActivityH5Binding onSupplyViewBinding(@NonNull LayoutInflater layoutInflater) {
        return ActivityH5Binding.inflate(layoutInflater);
    }

    @Override
    protected TitleBar onSupplyTitleBar() {
        return currentViewBinding().tbTitle;
    }

    @Override
    protected H5Presenter onSupplyPresenter() {
        return new H5Presenter();
    }

    @Override
    protected void doOnCreate(@Nullable Bundle savedInstanceState) {
        super.doOnCreate(savedInstanceState);
        this.savedInstanceState = savedInstanceState;

        invokeController = new InvokeController();
        invokeController.setDebug(true);
    }

    @Override
    protected void doOnResume() {
        super.doOnResume();
        invokeController.onResume();
    }

    @Override
    protected void doOnStop() {
        super.doOnStop();
        invokeController.onStop();
    }

    @Override
    protected void doOnStart() {
        super.doOnStart();
        invokeController.onStart();
    }

    @Override
    protected void doOnPause() {
        super.doOnPause();
        invokeController.onPause();
    }

    @Override
    protected void onSaveInstanceState(@NonNull Bundle outState) {
        super.onSaveInstanceState(outState);
        this.savedInstanceState = outState;
        invokeController.onSaveInstanceState(outState);
    }

    @Override
    protected void doOnActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.doOnActivityResult(requestCode, resultCode, data);
        invokeController.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        invokeController.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

    @Override
    protected void doOnBackPressed() {
        invokeController.onBackPressed();
    }

    @Override
    protected void doOnDestroy() {
        super.doOnDestroy();
        invokeController.onDestroy();
    }

    private void showErrorTips() {
        SnackbarManager.show(
                Snackbar
                        .with(this)
                        .position(Snackbar.SnackbarPosition.BOTTOM_CENTER)
                        .colorResource(R.color.base_color_red)
                        .text("连接失败")
                        .actionLabel("重试")
                        .actionListener(snack -> {
                            invokeController.load(currentPresenter().getUrl());
                            snack.dismiss();
                        })
                );
    }

    public void loadH5() {
        LazyH5API api = new LazyH5API(this, invokeController, currentPresenter().getUrl());

        invokeController.addAPI(api, H5Presenter.NAME_SPACE);

        invokeController.setPageLoadListener(new InvokeController.PageLoadListener() {
            @RequiresApi(api = Build.VERSION_CODES.M)
            @Override
            public void onPageStart(String s) {
                currentViewBinding().linearProgress.setVisibility(View.VISIBLE);
                currentViewBinding().linearProgress.setIndicatorColor(
                        getColor(R.color.color_statue_bar),
                        getColor(R.color.color_statue_bar),
                        getColor(R.color.color_statue_bar)
                );
            }

            @Override
            public void onPageFinished(String s) {
                currentViewBinding().linearProgress.setVisibility(View.GONE);
            }

            @Override
            public void onPageError(int i, String s) {
                currentViewBinding().linearProgress.setVisibility(View.GONE);
                showErrorTips();
            }
        });
        invokeController.onCreate(this, savedInstanceState, currentViewBinding().wvH5);
        invokeController.load(currentPresenter().getUrl());
    }

    public void updateTitle(boolean useAndroidTitle, String title) {
        if (TextUtils.isEmpty(title) || !useAndroidTitle) {
            currentViewBinding().tbTitle.setVisibility(View.GONE);
        } else {
            currentViewBinding().tbTitle.setVisibility(View.VISIBLE);
            currentViewBinding().tbTitle.setTitle(title);
        }
    }
}
